package histogram;

import java.util.Arrays;

public class HistogramCreater {

    final int image[][];
    HistogramDescr histogram;
    int bins = 64;
    int window = 60;
    int offset = 10;

    public HistogramCreater(final int[][] image) {
        System.out.println("HistogramCreater......");
        this.image = image;

        int width = image.length;
        int height = image[0].length;

        System.out.println("image size");
        System.out.println(width + " " + height);

        histogram = new HistogramDescr(width, height, bins, window, offset);
        countHistogram();
    }

    public HistogramDescr getHistogram() {
        return histogram;
    }

    void countHistogram() {

        for (int xSt = 0; xSt < histogram.width - window; xSt += histogram.offset) {
            for (int ySt = 0; ySt < histogram.height - window; ySt += histogram.offset) {

                double binVals[] = new double[histogram.bins];
                Arrays.fill(binVals, 0);

                for (int i = xSt; i < xSt + window; i++) {
                    for (int j = ySt; j < ySt + window; j++) {
                        int val = image[i][j];
                        binVals[val / histogram.divisor]++;
                    }
                }

                for (int i = 0; i < histogram.bins; i++) {
                    binVals[i] /= (window * window);
                }

                histogram.xList.add(xSt);
                histogram.yList.add(ySt);
                histogram.valueList.add(binVals);
            }
        }


    }
}
